﻿@{ 
    Layout = "~/Views/Shared/_Form.cshtml";
}
<style>
    .form-control {
        background-color: #FAFAD2;
        margin-bottom: 5px !important;
    }
    .form-group {
        margin-bottom: 0;
    }
    .m-b-none{
        margin-bottom:5px;
    }
</style>
<form id="form" class="form-horizontal m" role="form">
    <div class="col-sm-12 text-center">
        <span class="help-block m-b-none text-red"><i class="fa fa-info-circle"></i> 除公务、课酬、其他项外，成本都按【数量*单价】汇总</span>
    </div>
</form>
<script type="text/javascript">
    var itemId = parseInt(frame.request("id")) || 0;
    var segmentId = parseInt(frame.request("segmentId"));
    var priceId = frame.request("priceId");
    var code = frame.request("code"); 
    $(function () {  
        let filterData = parent.PageUI.initValue.QuotationActivityDetail.filter(x => x.Code == code && x.SegmentId == segmentId && x.ItemId == itemId);//过滤选中项的数据
        if (filterData.length != 0) {
            renderData(filterData, false);
        }
        else {
            frame.ajax({
                url: '@Url.Content("/Sale/BaseInfo/GetBaseActivityDetail")' + '?id=' + itemId,
                type: 'get', 
                success: function (obj) {
                    if (obj.success) {
                        if (obj.data.length != 0) { 
                            renderData(obj.data,true);
                        }
                        else {
                            frame.msgWarning("暂无相关基础参数项！");
                        }
                    }
                }
            })
        }
    });

    function renderData(data,isinit) {
        var html = [];
        $.each(data, function (i, n) {
            var star = "";
            if (!n.Mark && !!n.ItemName) { 
                star = "<i class=\"fa fa-star text-orange\"/></i>";
            }

            html.push("<div class=\"form-group\" mark=\"" + (n.Mark ? 1 : 0) + "\">");
            html.push(`<label class=\"col-sm-3 control-label\" name="${n.ItemName ?? ""}">${star}${n.ItemName ?? ""}</label>`);
            html.push("<div class=\"col-sm-4\">");
            if (!n.Mark) {
                html.push(`<input type=\"text\" name="itemCostName" class=\"form-control\" value="${(n.ItemCostName ?? "")}" placeholder="成本名称" />`);
            }
            else {
                html.push(`<input type=\"number\" name="quantity" class=\"form-control\" value="${(!!n.ItemQty ? n.ItemQty : "")}" placeholder="数量" />`);
            }
            html.push("</div>");
            html.push("<div class=\"col-sm-4\">");
            html.push(`<input type=\"number\" name="price" class=\"form-control\" value="${(!!n.ItemCostPrice ? n.ItemCostPrice : "")}" placeholder="成本单价"/>`);
            if (n.Mark) {
                html.push(`<input type=\"hidden\" name="salePrice" value="${(!!n.ItemSalePrice ? n.ItemSalePrice : "")}"/>`);
            }
            html.push("</div>");

            if (!n.Mark) {
                if (!!n.ItemName) {
                    html.push("<div class=\"col-sm-1\">");
                    html.push("<a class=\"fa fa-plus-circle fa-lg text-orange\" onclick=\"addRow($(this),\'" + n.ItemName + "\')\"  href=\"javascript:void(0);\"></a>");
                    html.push("</div>");
                }
                else {
                    html.push("<div class=\"col-sm-1\">");
                    html.push("<a class=\"fa fa-minus-circle fa-lg text-red\" onclick=\"delRow($(this))\"  href=\"javascript:void(0);\"></a>");
                    html.push("</div>");
                }
            }
            html.push("</div>");
        }); 
        $('#form').append(html.join(''));
    }

    function addRow($this,name) {
        var $tr = $this.parent().parent();
        var html = [];
        html.push("<div class=\"form-group\" mark=\"0\">");
        html.push(`<label class=\"col-sm-3 control-label\"></label>`);
        html.push("<div class=\"col-sm-4\">");
        html.push(`<input type=\"text\" name="itemCostName" class=\"form-control\" value="" placeholder="成本名称" />`);
        html.push("</div>");
        html.push("<div class=\"col-sm-4\">");
        html.push(`<input type=\"number\" name="price" class=\"form-control\" value="" placeholder="成本合计"/>`);
        html.push("</div>");
        html.push("<div class=\"col-sm-1\">");
        html.push("<a class=\"fa fa-minus-circle fa-lg text-red\" onclick=\"delRow($(this))\"  href=\"javascript:void(0);\"></a>");
        html.push("</div>");
        html.push("</div>");
        $tr.after(html.join(''));
    }

    function delRow($this) {
        $this.parent().parent().remove();
    }

    function saveForm(index) {
        var total = 0, qty = 0, price = 0; 
        parent.PageUI.initValue.QuotationActivityDetail = parent.PageUI.initValue.QuotationActivityDetail.filter(x => !(x.Code == code && x.SegmentId == segmentId && x.ItemId == itemId));//筛选出非当前的明细赋值给当前数据  实现移除原数据的效果
        $('div[class="form-group"]').each(function () {
            var $this = $(this);
            price = parseFloat($this.find('input[name="price"]').val()) || 0; 
            if ($this.attr('mark') == "0") { 
                parent.PageUI.initValue.QuotationActivityDetail.push(
                    {
                        Code: code,
                        SegmentId: segmentId,
                        ItemId: itemId,//下拉选项Id
                        ItemName: $this.find('label').attr('name'),
                        ItemCostName: $this.find('input[name="itemCostName"]').val(),
                        ItemCostPrice: price,
                        ItemSalePrice: null,
                        ItemQty: 1,
                        Mark: false,
                    });
                total = total + price;
            }
            else { 
                qty = parseFloat($this.find('input[name="quantity"]').val()) || 0;
                parent.PageUI.initValue.QuotationActivityDetail.push(
                    {
                        SegmentId: segmentId,
                        ItemId: itemId,//下拉选项Id
                        ItemName: $this.find('label').attr('name'),
                        ItemCostName: "",
                        ItemCostPrice: price,
                        ItemSalePrice: parseFloat($this.find('input[name="salePrice"]').val()) || 0,
                        ItemQty: qty,
                        Mark: true,
                    });
                total = total + qty * price;
            }  
        });
        debugger
        console.log(parent.PageUI.initValue.QuotationActivityDetail)
        parent.document.getElementById(priceId).value = total.toFixed(2);
        parent.PageUI.triggerForPageInputEvent(priceId);
        parent.layer.close(index);
    }
</script>